package data.join.com.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.MediaMetadataRetriever;
import android.text.TextUtils;

import java.io.File;

import data.join.com.database.bean.VideoBean;

/**
 * Created by Administrator on 2017/9/18.
 */

public class VideoUtils {

    public static String findVideoThumbnail(String videoPath){
        String tmpPath;

        tmpPath = getCacheVideoThumbnail(videoPath);

        if(TextUtils.isEmpty(tmpPath)){
            tmpPath = getDecodeVideoThumbnail(videoPath);
        }

        return tmpPath;
    }

    public static String getCacheVideoThumbnail(String videoPath){
        String tmpPath = null;

        String cacheDir = "/storage/sdcard1/DVR/~cache";

        if(!TextUtils.isEmpty(videoPath)) {
            File file = new File(videoPath);
            String fileName = file.getName();
            String cacheName = fileName.replace("ts","jpg");
            String cachePath = cacheDir + File.separator + cacheName;
            File cacheFile = new File(cachePath);

            if(cacheFile.exists()){
                tmpPath = cacheFile.getAbsolutePath();
            }
        }

        //MyLog.D("getCacheVideoThumbnail tmpPath="+tmpPath);

        return tmpPath;
    }

    public static String getDecodeVideoThumbnail(String videoPath) {
        String tmpPath = null;

        try {
            MediaMetadataRetriever media = new MediaMetadataRetriever();

            media.setDataSource(videoPath);

            Bitmap bitmap = media.getFrameAtTime();

            tmpPath = ImageUtils.saveBitmapAsTmpImgFile(small(bitmap));

            media.release();
        }catch (Exception e){
            e.printStackTrace();
        }

        return tmpPath;
    }

    public static Long getVideoDurationPath(String videoPath) {
        Long duration = 0L;

        String dur = null;
        try {
            MediaMetadataRetriever media = new MediaMetadataRetriever();
            media.setDataSource(videoPath);
            dur = media.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            media.release();
        }catch (Exception e){
            e.printStackTrace();
        }

        if(!TextUtils.isEmpty(dur)){
            try {
                duration = Long.parseLong(dur);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return duration;
    }

    public static boolean getVideoInfo(VideoBean videoBean) {
        boolean ret = true;

        String tmpPath = null;
        String dur = null;

        try {
            MediaMetadataRetriever media = new MediaMetadataRetriever();

            media.setDataSource(videoBean.srcPath);

            Bitmap bitmap = media.getFrameAtTime();

            tmpPath = ImageUtils.saveBitmapAsTmpImgFile(small(bitmap));

            dur = media.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);

            media.release();
        }catch (Exception e){
            e.printStackTrace();
            ret = false;
        }

        try {
            videoBean.duration = Long.parseLong(dur);
        }catch (Exception e){
            e.printStackTrace();
            ret = false;
        }

        videoBean.thumbnail = tmpPath;

        return ret;
    }


    private static Bitmap small(Bitmap bitmap) {
        Matrix matrix = new Matrix();

        float scale = computeScale(bitmap,640,360);

        matrix.postScale(scale, scale);

        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        return resizeBmp;
    }

    public static VideoBean getVideoInfoByPath(File file){
        VideoBean videoBean;

        videoBean = new VideoBean();

        videoBean.srcPath = file.getAbsolutePath();

//        videoBean.thumbnail = getCacheVideoThumbnail(videoBean.srcPath);
//
//        if(TextUtils.isEmpty(videoBean.thumbnail)){
//            getVideoInfo(videoBean);
//        }else {
//            videoBean.duration = getVideoDurationPath(videoBean.srcPath);
//        }

        videoBean.duration = getVideoDurationPath(videoBean.srcPath);

        return videoBean;
    }


    /**
     * 根据View(主要是ImageView)的宽和高来计算Bitmap缩放比例。默认不缩放
     *
     * @param src
     */
    private static float computeScale(Bitmap src, int viewWidth, int viewHeight) {
        float inSampleSize = 0.5f;
        if (viewWidth == 0 || viewWidth == 0) {
            return inSampleSize;
        }
        int bitmapWidth = src.getWidth();
        int bitmapHeight = src.getHeight();

        // 假如Bitmap的宽度或高度大于我们设定图片的View的宽高，则计算缩放比例
        if (bitmapWidth > viewWidth || bitmapHeight > viewHeight) {
            int widthScale = Math.round((float) viewWidth / (float) bitmapWidth );
            int heightScale = Math.round((float) viewHeight / (float) bitmapHeight );

            int tempScale = widthScale > heightScale ? widthScale : heightScale;
            if(tempScale < inSampleSize)
                inSampleSize = tempScale;
        }

        //MyLog.D("computeScale inSampleSize="+inSampleSize);

        return inSampleSize;
    }
}
